cmake_minimum_required(VERSION 2.8)

PROJECT(dmlua)

INCLUDE(cmake/ModuleImport.cmake)
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
INCLUDE(cmake/ModuleCompileOptions.cmake)
ModuleSetCompileOptions()

OPTION(USE_DMLUA_INNER "use lua inner" ON)

ADD_DEFINITIONS(-DLUA_COMPAT_MODULE)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/lua)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src/tolua)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test)

#FILE(GLOB_RECURSE DMLUA_SOURCES
    #${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
    #${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

#ADD_LIBRARY(dmlua ${DMLUA_SOURCES})

FILE(GLOB_RECURSE TOLUA_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/tolua/*.h)

ADD_LIBRARY(tolua ${TOLUA_SOURCES})

FILE(GLOB DMLUA_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/*.h

)

LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "lua.c$")
LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "luac.c$")
LIST(FILTER DMLUA_SOURCES EXCLUDE REGEX "wmain.c$")

FILE(GLOB LUA_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/lua/lua.c
)

IF (WIN32)
    IF (USE_DMLUA_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-DLUA_BUILD_AS_DLL -DLUA_CORE")
    ENDIF(USE_DMLUA_INNER)

    TARGET_LINK_LIBRARIES(tolua)
ELSEIF (APPLE)
    IF (USE_DMLUA_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53 dl)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-Wl,-E")
    ENDIF(USE_DMLUA_INNER)
    TARGET_LINK_LIBRARIES(tolua m dl)
ELSEIF (UNIX)
    IF (USE_DMLUA_INNER)
        ADD_LIBRARY(lua53 SHARED ${DMLUA_SOURCES})
        ADD_EXECUTABLE(lua ${LUA_SOURCES})
        TARGET_LINK_LIBRARIES(lua lua53 m dl)
        SET_TARGET_PROPERTIES(lua53 PROPERTIES COMPILE_FLAGS "-Wl,-E")
    ENDIF(USE_DMLUA_INNER)
    TARGET_LINK_LIBRARIES(tolua m dl)
ENDIF ()

FILE(GLOB_RECURSE TOLUA++_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/tolua.c
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/toluabind.c
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/*.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/*.h)

ADD_EXECUTABLE(tolua++ ${TOLUA++_SOURCES})
TARGET_LINK_LIBRARIES(tolua++ lua53 tolua)

FILE(GLOB_RECURSE DMLUATEST_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/test/*.pkg
    ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

ADD_EXECUTABLE(dmluatest ${DMLUATEST_SOURCES})
TARGET_LINK_LIBRARIES(dmluatest lua53 tolua)

IF (WIN32)
ADD_CUSTOM_COMMAND(
    TARGET dmluatest
    PRE_BUILD
    COMMAND tolua++ -t -n interface -o interface.cpp -H interface.h interface.pkg
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test/script
    DEPENDS tolua++
    )
ENDIF(WIN32)

FILE(GLOB_RECURSE LUACHECK_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/tool/luacheck/*.pkg)

ADD_EXECUTABLE(dmluacheck ${LUACHECK_SOURCES})
TARGET_LINK_LIBRARIES(dmluacheck lua53 tolua)


